<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>${SYSTEM_NAME}</title>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/layui/css/layui.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/style/admin.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/zTree_v3/css/metroStyle/metroStyle.css?v=${FILE_VERSION}" type="text/css">
</head>
<body>
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-body" style="padding: 15px;">
		<form class="layui-form" action="" style="width:100%" method="post" name="user-form">
			<input type="text"  name="test1" style="display: none;" />
			<input type="password" name="test2" style="display: none;"/>
			<input type="hidden" name="id" id="id" value="${user.id }"/>
			<div class="layui-row">
				<div class="layui-col-lg6">
					<c:if test="${MUTI_ORG}">
					<div class="layui-form-item">
						<label class="layui-form-label layui-form-required">所属机构</label>
						<c:if test="${userType != 3}">
							<div class="layui-input-inline">
								<input type="hidden" id="orgId" name="orgId"value="${user.orgId}">
								<input type="text" id="orgName" class="layui-input" readonly="readonly" value="${user.orgName}"
									   lay-verify="required" placeholder="点击选择所属机构" onclick="selectOrg()">
							</div>
							<div class="layui-input-inline">
								<button type="button" class="layui-btn layui-btn-primary layui-border-blue" onclick="selectOrg()">点击选择</button>
							</div>
						</c:if>
						<c:if test="${userType == 3}">
							<div class="layui-input-inline">
								<input type="hidden" name="orgId" value="${user.orgId}" >
								<input type="text" class="layui-input" readonly="readonly" value="${user.orgName}"
									   lay-verify="required" >
							</div>
						</c:if>
					</div>
					</c:if>

				  <div class="layui-form-item">
					<label class="layui-form-label layui-form-required">登录账号</label>
					<div class="layui-input-inline">
						<input id="oldname" type="hidden" value="${user.username}"  autocomplete="new-oldName">
					    <input id="username" type="text" name="username" lay-verify="username"  autocomplete="new-userName" placeholder="请输入登录账号" class="layui-input" autocomplete="off" value="${user.username}">
					</div>
					<div class="layui-form-mid layui-word-aux">登录账号最小不能小于6位</div>
				  </div>
					<c:if test="${user.id == null}">
					  <div class="layui-form-item">
						<label class="layui-form-label layui-form-required">密码</label>
						<div class="layui-input-inline">
							<input id="pwd" type="password" name="password"  autocomplete="new-password" lay-verify="pass" placeholder="请输入密码" class="layui-input" autocomplete="off">
						</div>
						  <c:if test="${pwdStrength == 0}">
							  <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
						  </c:if>
						  <c:if test="${pwdStrength == 1}">
							  <div class="layui-form-mid layui-word-aux">包含6-16个字符，至少1个字母和1个数字</div>
						  </c:if>
						  <c:if test="${pwdStrength == 2}">
							  <div class="layui-form-mid layui-word-aux">包含6-16个字符，至少1个大写字母，1个小写字母和1个数字</div>
						  </c:if>
					  </div>
					  <div class="layui-form-item">
						<label class="layui-form-label layui-form-required">验证密码</label>
						<div class="layui-input-inline">
						  <input type="password" name="repassword" lay-verify="rePassword" placeholder="请输入密码" class="layui-input">
						</div>
						<div class="layui-form-mid layui-word-aux">验证密码必须和密码一致</div>
					  </div>
					</c:if>
					<div class="layui-form-item">
						<label class="layui-form-label<c:if test="${LOGIN_SMS_CODE_OPEN == '1'}"> layui-form-required</c:if>">手机号</label>
						<input id="oldPhone" type="hidden" value="${user.phone}">
						<div class="layui-input-inline">
							<input type="text" name="phone"
							<c:choose><%--系统配置开启短信验证码登录方式的话，手机号必填，没开启时非必填--%>
								<c:when test="${LOGIN_SMS_CODE_OPEN == '1'}">
								   lay-verify="required|phone|checkPhone"
								</c:when>
								<c:otherwise>
								   lay-verify="phoneAllowNull|checkPhone"
								</c:otherwise>
							</c:choose>
								   placeholder="请输入手机号" class="layui-input" value="${user.phone }">
						</div>
						<div class="layui-form-mid layui-word-aux">手机号用于短信验证码登录</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">真实姓名</label>
						<div class="layui-input-inline">
							<input type="text" name="realName" lay-verify="realName" placeholder="请输入真实姓名" class="layui-input" value="${user.realName }">
						</div>
						<div class="layui-form-mid layui-word-aux">请输入真实姓名</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label layui-form-required">权限范围</label>
						<div class="layui-input-inline layui-form">
							<select  lay-verify="required" name="type" lay-filter="typeFilter">
								<c:forEach items="${typeMap}" var="t">
									<%--开发者才能选择所有机构权限--%>
									<c:if test="${t.key!='1' || (t.key=='1' && isDeveloper=='1')}">
										<option value="${t.key }" <c:if test="${t.key eq user.type}">selected="selected"</c:if>>${t.value }</option>
									</c:if>
								</c:forEach>
							</select>
						</div>
					</div>
<%--					<div class="layui-form-item">--%>
<%--						<div class="layui-inline">--%>
<%--						  <label class="layui-form-label layui-form-required">手机</label>--%>
<%--						  <div class="layui-input-inline">--%>
<%--							<input type="tel" name="phone" lay-verify="required|phone" class="layui-input" value="${user.phone }" placeholder="请输入手机号码">--%>
<%--						  </div>--%>
<%--						</div>--%>
<%--					</div>--%>

				  <div class="layui-form-item">
					<label class="layui-form-label">身份证号</label>
					<div class="layui-input-inline">
					  <input type="text" name="idCard" lay-verify="" placeholder="请输入身份证号" class="layui-input" value="${user.idCard }" placeholder="请输入身份证号码">
					</div>
				  </div>
				</div>
				<div class="layui-col-lg6">
					<div class="layui-form-item layui-form-text" >
						权限范围中选择自定义机构后可设置机构
					</div>
					<div class="layui-form-item layui-form-text" id="orgIdsSelect" style="display: none">
						<ul id="treeRegion" class="ztree">
							机构加载中....
						</ul>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn layui-btn-primary layui-border-blue" lay-submit="" lay-filter="sub">立即提交</button>
						<button type="reset" class="layui-btn layui-btn-primary layui-border-blue">重置</button>
						<button type="button" class="layui-btn layui-btn-primary layui-border-red" onclick="closed()">关闭</button>
					</div>
				</div>
		  </div>
		</form>
  	  </div>
	</div>
</div>
<script src="${pageContext.request.contextPath}/static/js/common-2.0.0.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/userCheck/user-check.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/layuiadmin/layui/layui.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/common/coreInput-2.0.0.js?v=${FILE_VERSION}"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.core.js?v=${FILE_VERSION}"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.excheck.js?v=${FILE_VERSION}"></script>
<script>
	layui.config({
		base: getContextPath() + '/static/layuiadmin/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['form'], function() {
	var form = layui.form;
	var pwdStrength = ${pwdStrength};
	form.verify({
	  username: function(value){ //value：表单的值、item：表单的DOM对象
	    if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
	      return '登录账号不能有特殊字符';
	    }
	    if(/(^\_)|(\__)|(\_+$)/.test(value)){
	      return '登录账号首尾不能出现下划线\'_\'';
	    }
	    if($("#oldname").val() != value && value){
		   var msg = checkUserName(value);
		   if(msg.code != 0 ){return "登录账号已存在，请重新输入";}
	    }
	  }
	  ,pass: function(value, item) {
			return systemUserPwdCheck(pwdStrength, value);
	  }
	  ,rePassword : function(value, item){
		  var passValue = $("#pwd").val();
		  if(passValue != value){
	      	return '两次密码输入不一致';
	      }
	  },phoneAllowNull : function(value){
	  	    //允许为空，layui固有的phone不允许为空
			if(value && !/(^$)|^1(3|4|5|7|8)\d{9}$/.test(value)){
				return "请输入正确的手机号";
			}
		},checkPhone: function(value){
			if($("#oldPhone").val() != value && value){
				var msg = checkUserPhone(value);
				if(msg.code != 0 ){return "手机号已绑定其他用户，请重新输入";}
			}
		}
	});

	form.on('select(typeFilter)', function(data) {
		if(data.value != '4'){
			$("#orgIdsSelect").hide();
		}else{
			$("#orgIdsSelect").show();
		}
	})

	
	form.on('submit(sub)', function(data){
		if(data.field["type"] != ''){
			var treeObj = $.fn.zTree.getZTreeObj("treeRegion");
			var nodes = treeObj.getCheckedNodes(true);
			var ids = "";
			for(var i=0; i<nodes.length; i++){
				ids += nodes[i].id + ",";
			}
			if(nodes.length != 0){
				ids = ids.substr(0,ids.length-1);
			}
			data.field["orgIds"] = ids;
		}else{
			data.field["orgIds"] = '';
		}
		return saveOrUpdate($("#id").val(),"/sys/user",data);
	});

});


function checkUserName(username){
	var result= null;
	$ajax({
		url: "/sys/user/add/checkUser",
		data: {
			"username":username
		},
		async: false,
		succCallback: function (json) {
			result= json;  //return放在这里返回不了的
		}
	});
	return result;
}

	function checkUserPhone(phone){
		var result= null;
		$ajax({
			url: "/sys/user/add/checkUser",
			data: {
				"phone":phone
			},
			async: false,
			succCallback: function (json) {
				result= json;  //return放在这里返回不了的
			}
		});
		return result;
	}

<c:if test="${MUTI_ORG}">
function selectOrg(){
	var url  = getContextPath() +  "/sys/org/setOrg?queryTop=true";
	layer.open({
		title : "选择机构",
		type: 2,
		area: ['500px', '90%'],
		anim: 2,
		content: [url, 'yes']
	});
}

function setOrg(id,name){
	$("#orgId").val(id);
	$("#orgName").val(name);
	layer.closeAll();
}
</c:if>
var ZtreeObj;
var updateTableCell;
var setting = {
	check: {enable: true, chkStyle: "checkbox", chkboxType: { "Y": "", "N": "" }},
	data: {simpleData: {enable: true}},
	edit: {enable: false},
	callback : {onClick: treeOnClick}
};


function treeOnClick(event, treeId, treeNode) {
	parentName = treeNode.name;
	reload(treeNode.id);
}

$(document).ready(function(){
	$.ajax({
		url : getContextPath() + '/sys/user/add/orgInfo',
		data: {
			"projectId" : $("#id").val()
		},
		type : "post",
		dataType : "text",
		success : function(data) {
			var zNodes = eval("(" + data + ")");
			ZtreeObj = $.fn.zTree.init($("#treeRegion"), setting, zNodes);
		},
		error : function(XMLHttpRequest, textStatus) {
			layer.msg(textStatus);
		}
	});
});
</script>
</body>
</html>